package com.aynu.gulimall.member.service.impl;

import com.aynu.gulimall.member.dao.MemberLevelDao;
import com.aynu.gulimall.member.entity.MemberLevelEntity;
import com.aynu.gulimall.member.exception.PhoneExistException;
import com.aynu.gulimall.member.exception.UserNameExistException;
import com.aynu.gulimall.member.vo.MemberLoginVo;
import com.aynu.gulimall.member.vo.MemberRegisterVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.aynu.common.utils.PageUtils;
import com.aynu.common.utils.Query;

import com.aynu.gulimall.member.dao.MemberDao;
import com.aynu.gulimall.member.entity.MemberEntity;
import com.aynu.gulimall.member.service.MemberService;
import org.springframework.transaction.annotation.Transactional;


@Service("memberService")
@Slf4j
public class MemberServiceImpl extends ServiceImpl<MemberDao, MemberEntity> implements MemberService {

    @Autowired
    private MemberLevelDao memberLevelDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<MemberEntity> page = this.page(
                new Query<MemberEntity>().getPage(params),
                new QueryWrapper<MemberEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void register(MemberRegisterVo vo) {
        log.info("查询会员默认等级");
        MemberLevelEntity memberLevelEntity = memberLevelDao.getDefaultLevel();

        //检查用户名和手机号唯一性
        checkPhoneUnique(vo.getPhone());
        checkUserNameUnique(vo.getUserName());

        //封装数据
        MemberEntity memberEntity = new MemberEntity();
        memberEntity.setUsername(vo.getUserName());
        memberEntity.setMobile(vo.getPhone());

        //使用MD5盐值加密
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        String password = encoder.encode(vo.getPassword());
        memberEntity.setPassword(password);

        //其他的默认值

        log.info("用户信息注册，{}",memberEntity);
        MemberDao baseMapper = this.getBaseMapper();
        baseMapper.insert(memberEntity);
    }

    @Override
    public void checkPhoneUnique(String phone) throws PhoneExistException {
        MemberDao baseMapper = this.getBaseMapper();
        Integer num = baseMapper.selectCount(new QueryWrapper<MemberEntity>().eq("mobile", phone));
        if (num>0){
            throw new PhoneExistException();
        }
    }

    @Override
    public void checkUserNameUnique(String userName) throws UserNameExistException {
        MemberDao baseMapper = this.getBaseMapper();
        Integer num = baseMapper.selectCount(new QueryWrapper<MemberEntity>().eq("username", userName));
        if (num>0){
            throw new PhoneExistException();
        }
    }

    @Override
    public MemberEntity login(MemberLoginVo vo) {
        log.info("根据用户名或手机号");
        MemberDao baseMapper = this.getBaseMapper();
        MemberEntity memberEntity = baseMapper.selectOne(new QueryWrapper<MemberEntity>()
                .eq("username", vo.getLoginacct()).or().eq("mobile", vo.getLoginacct()));
        if (memberEntity==null){
            return null;
        }else {
            //获取数据库的password
            String password = memberEntity.getPassword();
            BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
            boolean matches = encoder.matches(vo.getPassword(), password);
            //密码匹配
            if (matches){
                return memberEntity;
            }else {
                return null;
            }
        }
    }

}